package com.meilai.project.controller.web.login;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.system.ResetPasswordDTO;
import com.meilai.project.service.system.LoginService;
import com.meilai.project.service.system.UserLoginLogService;
import com.meilai.project.service.system.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;


@Api(value = "web端登录操作", description = "web端登录")
@RestController
@RequestMapping("api/web/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserLoginLogService userLoginLogService;

    @Autowired
    private LoginService loginService;

    @ApiOperation(value = "登录")
    @PostMapping("sign-in")
    public CommonResult login(
            HttpServletRequest request,
            @RequestParam(name = "username") String username,
            @RequestParam("password") String password){
        return new CommonResult().success(loginService.webLogin(request, username, password));
    }

    @PutMapping("sign-out")
    @ApiOperation(value = "登出")
    public CommonResult signOut(){
        return new CommonResult().success();
    }

    @GetMapping("info")
    @ApiOperation(value = "用户详情")
    public CommonResult adminInfo(HttpServletRequest request){
        Long user_id = Long.parseLong(request.getAttribute("tokenUserId").toString());
        return new CommonResult().success(userService.findDtoById(user_id));
    }

    @PutMapping("reset_password")
    @ApiOperation(value = "重置密码")
    public CommonResult resetPassword(HttpServletRequest request, @Valid @RequestBody ResetPasswordDTO dto){
        Long user_id = Long.parseLong(request.getAttribute("tokenUserId").toString());
        return new CommonResult().success(userService.resetPassword(user_id,dto));
    }
}
